package pl.mchaniewski.ryneczek.pojo;

// Generated Oct 3, 2011 3:17:38 AM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "user", catalog = "stockmarket")
public class User implements java.io.Serializable {
    private static final long serialVersionUID = -1366766554929259749L;

    private Integer userid;
    private String login;
    private String pass;
    private Date lastLogin;
    private Date createDate;
    private Integer flags;
    private String identityNumber;
    private Set<Portfolio> portfolios = new HashSet<Portfolio>(0);

    public static final Integer DEFAULT_FLAGS = 128;
    public static final Integer IS_BLOCKED = 0;
    public static final Integer OPENID = 2;
    public static final Integer ROLE_ADMIN = 6;
    public static final Integer ROLE_USER = 7;
    public static final Integer ROLE_AUTHENTICATED = 8;

    public User() {
    }

    public User(Date createDate) {
        this.createDate = createDate;
    }

    public User(String login, String pass, Date lastLogin, Date createDate,
            Integer flags, String identityNumber, Set<Portfolio> portfolios) {
        this.login = login;
        this.pass = pass;
        this.lastLogin = lastLogin;
        this.createDate = createDate;
        this.flags = flags;
        this.identityNumber = identityNumber;
        this.portfolios = portfolios;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "USERID", unique = true, nullable = false)
    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    @Column(name = "LOGIN", length = 30)
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Column(name = "PASS", length = 30)
    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_LOGIN", length = 19)
    public Date getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_DATE", nullable = false, length = 19)
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Column(name = "FLAGS")
    public Integer getFlags() {
        return flags;
    }

    public void setFlags(Integer flags) {
        this.flags = flags;
    }

    @Column(name = "IDENTITY_NUMBER")
    public String getIdentityNumber() {
        return identityNumber;
    }

    public void setIdentityNumber(String identityNumber) {
        this.identityNumber = identityNumber;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    public Set<Portfolio> getPortfolios() {
        return portfolios;
    }

    public void setPortfolios(Set<Portfolio> portfolios) {
        this.portfolios = portfolios;
    }

    @Transient
    private boolean checkBit(int bitNumber) {
        int mask = 0x01;
        int result = (flags >> bitNumber) & mask;
        return (result == 0) ? false : true;
    }

    @Transient
    public boolean isActive() {
        return !checkBit(IS_BLOCKED);
    }

    @Transient
    public boolean hasFlagsSet(Integer... flag) {
        for (Integer i : flag) {
            if (!checkBit(i)) {
                return false;
            }
        }

        return true;
    }

    @Transient
    public void setUserFlags(boolean value, Integer... flag) {
        for (Integer i : flag) {
            if (checkBit(i) == value) {
                continue;
            } else {
                if (value) {
                    setFlags(getFlags() | (1 << i));
                } else {
                    setFlags(getFlags() - (1 << i));
                }
            }
        }
    }

    @Transient
    public List<String> getRoles() {
        List<String> roles = new LinkedList<String>();

        for (int i = 6; i <= 10; ++i) {
            if (checkBit(i)) {
                switch (i) {
                case 6:
                    roles.add("ROLE_ADMIN");
                    break;
                case 7:
                    roles.add("ROLE_USER");
                    break;
                case 8:
                    roles.add("ROLE_AUTHENTICATED");
                    break;

                default:
                    break;
                }
            }
        }

        return roles;
    }

}
